<?php

/**
 * Deleting an Update
 * @package ModuleAssistance
 * @version 1.0b
 * @var $_POST['id'] The update's ID
 */

session_start();

// Configuration file
include '../configuration.php';
// Functions
include '../inc/functions.php';

// Checking if user is logged in
if (!CheckSession()):
	exit();
endif;

// Include all objects
// TODO: Include only needed classes
foreach (glob($configuration['webroot'].'objects/*.php') as $class) { include_once $class; }

try {
	// Retrieving update
	$update = new Update();
	$update->Get($_POST['id']);
	
	// Deleting associated documents
	$documentList = $update->GetDocumentList();
	foreach ($documentList as $document) {
		
		// Deleting document from systemfiles
		$file = $configuration['webroot'].'data/docs/'.$document->documentId.'.'.$document->extension;
		if (file_exists($file))
			unlink($file);
	}
	
	// Deleting update from Database
	$update->Delete(false, true);
	
	echo 'OK';

} catch (Exception $e) {
	echo $e->getMessage();
}
?>